﻿using System;
using System.Collections.Generic;
using System.Text;
using Xeno.Core;
using Xeno.Maths;
using Xeno.Module;
using Xeno.GUI;

using OpenTK;
using OpenTK.Audio;

using OpenTK.Input;
using OpenTK.Platform;
using OpenTK;
using Komires.MataliPhysics;
namespace Xeno.Physics
{
    public class XenoPhysicsManager : XenoModule
    {
        public float TimeStep { get; set; }
      
        public float TimeMod { get; set; }

        public static XenoPhysicsManager Instance = null;

        public PhysicsEngine PE { get; set; }
        
        public PhysicsScene PS {get;set;}

        public bool DebugSceneVisually = true;

        public XenoPhysicsManager(string name) 
        {


            PE = new PhysicsEngine("XenoPhysicsManager_Engine001");
            PS = PE.Factory.PhysicsSceneManager.Create("XenoPhysicsManager_Scene001");


            Instance = this;


        }



        public CastHit Raycast(Vector3 origin, Vector3 dir, float dis)
        {
            /*
            float ox,oy,oz,dx,dy,dz;
            ox = origin.X;
            oy = origin.Y;
            oz = origin.Z;
            dx = dir.X;
            dy = dir.Y;
            dz = dir.Z;
            float hx,hy,hz;
            float nx,ny,nz;
            hx = hy=hz=0;
            nx =ny=nz = 0;
            
            bool hitBool = false;
           IntPtr shape = XP.RayCast(out ox,out oy,out oz,out dx,out dy,out dz,out dis,out hx,out hy,out hz,out nx,out ny,out nz,out hitBool);
           CastHit hit = new CastHit();
           if (hitBool)
           {
               hit.Hit = true;
               hit.ImpactNormal = new Vector3(nx, ny, nz);
               hit.ImpactPoint = new Vector3(hx, hy, hz);
               hit.Origin = origin;
               hit.NxShape = shape;
               if (shape.ToInt32() != 0)
               {
                   System.Diagnostics.Debug.WriteLine("Shape:" + shape.ToString());

                   hit.ImpactNode = XenoCollider.ShapeMap[shape];
               }
           }

            return hit;
            */
            return null;
        }
          
        public override void DoUpdate()
        {

            PS.Simulate(0.1f);

            //XP.SDKUpdate(0.03f);

            
        }

    }
   
    public class CastHit
    {
        public Vector3 ImpactPoint { get; set; }
        public bool Hit { get; set; }
        public Vector3 ImpactNormal { get; set; }
        public Vector3 Origin { get; set; }
        public IntPtr NxShape { get; set; }
        public XenoNode ImpactNode { get; set; }
    }

}
